﻿//**********************************************************
// TeletextService
//**********************************************************
Teletext = {};
Teletext.TeletextService = function()
{
	var _links = {red:0,green:1,yellow:2,blue:3,index:5};
	var _this = this;
	var _teletextProvider = window.top.OSH.getHost().Services.GetService("Osh.Teletext.ITeletextProvider");
	var _pageChangeSink = null;
	var _pageUpdateSink = null;
	
	this.onPageChange = new Event();	// get fired when the current page is changed during the navigation
	this.onPageUpdate = new Event();	// get fired when content of the current page is updated

	function initialize()
	{
		if(_teletextProvider)
		{
			_pageChangeSink = new EventSink(_teletextProvider, "PageChange", onPageChangeHandler);
			_pageUpdateSink = new EventSink(_teletextProvider, "PageUpdate", onPageUpdateHandler);
		
			var mediaController = window.top.OSH.getCurrent().mediaController;
			mediaController.registerService("Teletext", _this);
		}
	}
	
	function onPageChangeHandler(sender, args)
	{
		var e = window.top.document.createEventObject();
		e.page = _this.getCurrentPage();
		_this.onPageChange.fire(_this, e);
	}
	
	function onPageUpdateHandler(sender, args)
	{
		var e = window.top.document.createEventObject();
		e.page = _this.getCurrentPage();
		_this.onPageUpdate.fire(_this, e);
	}
	
	this.nextPage = function()
	{
		return _teletextProvider.NextPage(true, true);
	}

	this.previousPage = function()
	{
		return _teletextProvider.PreviousPage(true, true);
	}

	this.nextSubPage = function()
	{
		return _teletextProvider.NextPage(false, true);
	}

	this.previousSubPage = function()
	{
		return _teletextProvider.PreviousPage(false, true);
	}

	this.gotoPage = function(pageNumber, subPageNumber)
	{
		if(subPageNumber)
			return _teletextProvider.GoToPage(pageNumber, subPageNumber);
		else
			return _teletextProvider.GoToPage(pageNumber, -1);
	}

	this.gotoInitialPage = function()
	{
		_teletextProvider.GoToInitialPage();
	}

	// Button contains name of a naviation button: red, green, yellow or blue.
	this.followLink = function(button)
	{
		var link = _links[(button + "").toLowerCase()];
		if(link)
			_teletextProvider.FollowLink(link);
	}

	this.getCurrentPage = function()
	{
		return new Teletext.TeletextPage(_teletextProvider.CurrentPage, _teletextProvider.PageNumber, _teletextProvider.SubPageNumber);
	}

	initialize();
}
Type.registerType('Teletext.TeletextService');

Teletext.TeletextPage = function(_page, _pageNumber, _subPageNumber)
{
	this.getPageHtml = function()
	{
		if(_page)
			return _page.GetPageHtml();
		else
			return "";
	}
	
	this.getPageAddress = function()
	{
		return _pageNumber + (_subPageNumber > 0 ? "." + _subPageNumber : "");
	}
}
Type.registerType('Teletext.TeletextPage');

Teletext.TeletextService._current = new Teletext.TeletextService();
